package com.aglhz.nature.presenter;

import java.util.ArrayList;
import java.util.List;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import android.util.Log;


import com.aglhz.nature.constants.ServerAPI;
import com.aglhz.nature.events.MainViewEvent;
import com.aglhz.nature.modle.ColumnsListImage;
import com.aglhz.nature.modle.HeadScrollImage;
import com.aglhz.nature.modle.HomeRecommend;
import com.aglhz.nature.modle.HomeRecommend2;
import com.aglhz.nature.modle.item.ColumnsListData;
import com.aglhz.nature.modle.item.RecommendData;
import com.aglhz.nature.presenter.base.BasePresenter;
import com.aglhz.nature.view.iv.MainView;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

public class MainPresenter extends BasePresenter {
	private static String TAG = MainPresenter.class.getName();
	private List<ColumnsListData> cilumnslistData;
	private List<RecommendData> recommendData;
	// view
	private MainView mainView;

	// data
	private HeadScrollImage scrollImage;
	private ColumnsListImage columnsListImage;
	private HomeRecommend homeRecommend;
	private HomeRecommend2 homeRecommend2;
	public MainPresenter(MainView view) {
		mainView = view;
		// mSrcollModel = new Scroll222222222222222222();
	}

	// /*********************data operation*******************///
	public void loadData() {
		// load remote data
		this.loadScrollImage();
		this.loadColumnsListImage();
		this.loadHomeRecommend();
		this.loadHomeRecommend2();
		// save remote data

	}

	

	// /*********************view operation*******************///
	public void showSrcollView() {
		// get image's URL array list
		List<String>scrollImageUrls=new ArrayList<String>();
		
		for (int i = 0; i < scrollImage.getData().size(); i++) {
			scrollImageUrls.add(scrollImage.getData().get(i).getPictureUrl());
			
		}

		mainView.showSrcollImage(scrollImageUrls);
	}
	
	
	public void showColumnsListView(){
		mainView.setColumnsList(columnsListImage.getData());
	}

	public void showRecommedView() {
		mainView.seTRecommendList(homeRecommend.getData());
		
	}
	
	public String setUrl1(){
		return homeRecommend2.getData().getShowTypes().get(0).getPictureUrl();
	}
	public String setUrl2(){
		return homeRecommend2.getData().getShowTypes().get(1).getPictureUrl();
	}
	public String setUrl3(){
		return homeRecommend2.getData().getShowTypes().get(2).getPictureUrl();
	}
	
	public void showRecommed2View(){
		mainView.setRecommend2Pic();
	}

	public  String setNextTagUrl(){
		return homeRecommend2.getData().getTagURL();
		
	}
	public int setNextUrl1(){
		Log.e("--------------urlnext","-----------"+ homeRecommend2.getData().getShowTypes().get(0).getType());
		return homeRecommend2.getData().getShowTypes().get(0).getType();
		
	}
	
	public int setNextUrl2(){
		return homeRecommend2.getData().getShowTypes().get(1).getType();
	}
	
	public int setNextUrl3(){
		return homeRecommend2.getData().getShowTypes().get(2).getType();
	}
	
	// /*********************view operation*******************///
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onEventMainThread(MainViewEvent event) {

		String msg = event.getMessage();
		Log.i(TAG, "EventMainThread:" + msg);
		// update UI By event
		if (MainViewEvent.EVENT_UPDATE_SCROLL_IMAGE == msg) {
			// update scroll view
			this.showSrcollView();
			this.getBrindId();
		} else if (MainViewEvent.EVENT_UPDATE_COLUMNS_LIST == msg) {
			// update list view
			this.showColumnsListView();
		}else if (MainViewEvent.EVENT_UPDATE_HOMERECOMMEND_LIST == msg) {
			// update list view
			this.showRecommedView();
		}else if (MainViewEvent.EVENT_UPDATE_HOMERECOMMEND2_LIST == msg) {
			// update list view
			this.setUrl1();
			this.setUrl2();
			this.setUrl3();
			
			this.setNextTagUrl();
			this.setNextUrl1();
			this.setNextUrl2();
			this.setNextUrl3();
			this.showRecommed2View();
			
		}
		
	}



	///*********************private method*******************///
	private void loadScrollImage() {
		String url = ServerAPI.APIHEARD+ServerAPI.HOME_SCROLLVIEW;
		
		AsyncHttpClient client = new AsyncHttpClient();
		client.get(url, new TextHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString) {
				try {
					Log.e(TAG, "response" + responseString);

					Gson gson = new Gson();

					HeadScrollImage sBean = gson.fromJson(responseString,
							HeadScrollImage.class);
					scrollImage = (HeadScrollImage) sBean.clone();

					// send event
					MainViewEvent event = new MainViewEvent(
							MainViewEvent.EVENT_UPDATE_SCROLL_IMAGE);
					EventBus.getDefault().post(event);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString, Throwable throwable) {

			}

		});

	}
	
	
	///*********************private method*******************///	
	private void loadColumnsListImage() {

		String url =ServerAPI.APIHEARD+ServerAPI.HOME_COLUMS;

		AsyncHttpClient client = new AsyncHttpClient();
		client.post(url, new TextHttpResponseHandler() {
			
			@Override
			public void onSuccess(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString) {
				 try {
					 Log.e(TAG, "response" + responseString);
					
					 Gson gson = new Gson();
					
					ColumnsListImage cListImage = gson.fromJson(responseString,ColumnsListImage.class);
					columnsListImage = (ColumnsListImage) cListImage.clone();
					
					Log.d(TAG,"classificationImage---111-----"+columnsListImage.getOther());	
					cilumnslistData = columnsListImage.getData();
					// send event
					MainViewEvent event = new MainViewEvent(
							MainViewEvent.EVENT_UPDATE_COLUMNS_LIST);
					EventBus.getDefault().post(event);
					 
					 Log.e(TAG, "success" + "success");
					 } catch (Exception e) {
					 e.printStackTrace();
					 }
				
				
			}
			
			@Override
			public void onFailure(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString, Throwable throwable) {
				
				 Log.e(TAG, "failure");
			}
		

			 

	
		});

	}
		
	///*********************private method*******************///	
	public List<String> getBrindId(){
		// get image's URL array list
				List<String> BrindIds = new ArrayList<String>();
					for(int i=0;i<scrollImage.getData().size(); i++){
						BrindIds.add(scrollImage.getData().get(i).getBrandId());
						}
					
		return BrindIds;
		
		
	}
	///*********************private method*******************///		
		private void loadHomeRecommend() {
			String url = ServerAPI.APIHEARD+ServerAPI.HOME_RECOMMEND;

			AsyncHttpClient client = new AsyncHttpClient();
			client.get(url, new TextHttpResponseHandler() {
				
				@Override
				public void onSuccess(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString) {
					 try {
						 Log.e(TAG, "response" + responseString);
						
						 Gson gson = new Gson();
						
						HomeRecommend recommend = gson.fromJson(responseString,HomeRecommend.class);
						homeRecommend = (HomeRecommend) recommend.clone();
						
						
						recommendData = homeRecommend.getData();
						Log.d(TAG,"recommendData---111-----"+recommendData);	
						// send event
						MainViewEvent event = new MainViewEvent(
								MainViewEvent.EVENT_UPDATE_HOMERECOMMEND_LIST);
						EventBus.getDefault().post(event);
						 
						 Log.e(TAG, "success" + "success");
						 } catch (Exception e) {
						 e.printStackTrace();
						 }
					
					
				}
				
				@Override
				public void onFailure(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString, Throwable throwable) {
					
					 Log.e(TAG, "failure");
				}
			
		
			});
				
			}
	
		public void loadHomeRecommend2(){
			String url = ServerAPI.APIHEARD+ServerAPI.HOME_RECOMMEND2;

			AsyncHttpClient client = new AsyncHttpClient();
			client.get(url, new TextHttpResponseHandler() {
				
				@Override
				public void onSuccess(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString) {
					 try {
						 Log.e(TAG, "response" + responseString);
						
						 Gson gson = new Gson();
						
						HomeRecommend2 hoddrecommend = gson.fromJson(responseString,HomeRecommend2.class);
						homeRecommend2 = (HomeRecommend2) hoddrecommend.clone();
											
						Log.d(TAG,"homeRecommend2---111-----"+homeRecommend2);	
						// send event
						MainViewEvent event = new MainViewEvent(
								MainViewEvent.EVENT_UPDATE_HOMERECOMMEND2_LIST);
						EventBus.getDefault().post(event);
						 
						 Log.e(TAG, "success" + "success");
						 } catch (Exception e) {
						 e.printStackTrace();
						 }
					
					
				}
				
				@Override
				public void onFailure(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString, Throwable throwable) {
					
					 Log.e(TAG, "failure");
				}
			
		
			});						
	
		}
}
